package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQuery;
import cn.wolfcode.wolf2w.repository.StrategyThemeRepository;
import cn.wolfcode.wolf2w.repository.StrategyThemeRepository;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private StrategyThemeRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyTheme StrategyTheme) {
        repository.save(StrategyTheme);
    }



    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyTheme get(String id) {
        StrategyTheme StrategyTheme = repository.findById(id).get();
        return StrategyTheme;
    }

    @Override
    public List<StrategyTheme> list() {
        List<StrategyTheme> StrategyThemes = repository.findAll();
        return StrategyThemes;
    }

    //分页查询
    @Override
    public Page<StrategyTheme> query(StrategyThemeQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        long totalcount = template.count(query, StrategyTheme.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<StrategyTheme> list = template.find(query, StrategyTheme.class);
        return new PageImpl<>(list,pageable,totalcount);
    }


}
